package com.ccl.iot.huiju.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.ccl.iot.IOTInterface;
import com.ccl.iot.R;
import com.ccl.iot.batch.IOTBatch;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.device.IOTDeviceWithState;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.dvr.utils.GlideUtil;
import com.ccl.iot.dvr.utils.LogUtil;
import com.ccl.iot.huiju.CustomTextView;
import com.ccl.iot.huiju.EditItemFragment;
import com.ccl.iot.huiju.IOTHuiJu;
import com.ccl.iot.publics.UIUtils;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;

/**
 * @description：
 * @author： HD
 * @time：2017//22
 */

public class MainAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private SparseArray<IOTHuiJu> mDatas;
    public Context mContext;
    private int mDatasize;

    class ViewHolder {
        FrameLayout mSelectLayout;
        ImageView mItemImg;
        CustomTextView mItemNameTv;
    }

    public MainAdapter(Context context, SparseArray<IOTHuiJu> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
        mDatasize = datas.size();
    }

    public void notifyDataSetChanged(SparseArray<IOTHuiJu> datas) {
        mDatas = datas;
        mDatasize = datas.size();
        LogUtil.i(EditItemFragment.TAG, "size:" + mDatasize);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return 6;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;

        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_main, null);
            viewHolder.mSelectLayout = (FrameLayout) convertView.findViewById(R.id.layout_main_item);
            viewHolder.mItemImg = (ImageView) convertView.findViewById(R.id.item_iamge);
            viewHolder.mItemNameTv = (CustomTextView) convertView.findViewById(R.id.item_name);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        IOTHuiJu iotHuiJu = mDatas.get(position);
        if (iotHuiJu != null) {
            viewHolder.mItemImg.setVisibility(VISIBLE);
            viewHolder.mItemNameTv.setVisibility(VISIBLE);

            if (iotHuiJu instanceof IOTDevice) {
                final IOTDevice device = (IOTDevice) iotHuiJu;

                if (device instanceof IOTDeviceWithState) {
                    ((IOTDeviceWithState) device).setStateChangeListener(mStateChangeListener);
                }

                int imageId = device.getImageId();
                if (imageId != 0) {
                    GlideUtil.loadImageViewSize(mContext, imageId, 80, 80, viewHolder.mItemImg);
                }
                viewHolder.mItemNameTv.setText(device.getName(), Color.WHITE);

                viewHolder.mSelectLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        device.onViewClick();
                    }
                });

            }
            if (iotHuiJu instanceof IOTBatch) {
                final IOTBatch iotBatch = (IOTBatch) iotHuiJu;
                int imgIndex = iotBatch.getImgIndex();
                if (imgIndex < 0 || imgIndex >= Consts.BATCH_IMG_IDS.size()) {
                    imgIndex = 0;
                }
                GlideUtil.loadImageViewSize(mContext, Consts.BATCH_IMG_IDS.get(imgIndex), 80, 80, viewHolder.mItemImg);
                viewHolder.mItemNameTv.setText(iotBatch.getName(), Color.WHITE);

                viewHolder.mSelectLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        iotBatch.execute();
                    }
                });
            }
        } else {

            viewHolder.mItemImg.setVisibility(INVISIBLE);
            viewHolder.mItemNameTv.setVisibility(INVISIBLE);

        }

        return convertView;
    }



    private IOTInterface.IOTStateMonitorInterface.IOTStateChangeListener mStateChangeListener = new IOTInterface.IOTStateMonitorInterface.IOTStateChangeListener() {
        @Override
        public void onStateChanged(IOTInterface.IOTStateMonitorInterface aSource, int aNewState) {
            if (aSource != null && aSource instanceof IOTDevice) {
                IOTDevice dev = (IOTDevice) aSource;
                onDeviceChange(dev);
            }
        }
    };


    private void onDeviceChange(IOTDevice dev) {
        UIUtils.postTaskSafely(notifyRunnable);
    }

    public Runnable notifyRunnable = new Runnable() {
        @Override
        public void run() {
            notifyDataSetChanged();
        }
    };


    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    protected OnItemClickListener mItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }


}